import { defineStore } from 'pinia'
import defaultConfig from '@/config'
import { to } from 'await-to-js'

import { listConfig } from '@/api/system/config'
import { ConfigQuery, ConfigVO } from '@/api/system/config/types'

export const useConfigStore = defineStore('config', () => {
  const i18n = ref<boolean>(defaultConfig.i18n)
  const configs = ref<Array<ConfigVO>>([]) // 用户角色编码集合 → 判断路由权限

  // 获取用户信息
  const getConfigs = async (): Promise<void> => {
    const query: ConfigQuery = {}
    const [err, res] = await to(listConfig(query))
    if (res) {
      configs.value = res.data
      return Promise.resolve()
    }
    return Promise.reject(err)
  }

  return {
    i18n,
    getConfigs,
  }
})

export default useConfigStore
